package com.xxc.hs.funActivity.data;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xxc.hs.R;

@SuppressWarnings("ALL")
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyHolder> {
    private List<String> dataList = new ArrayList<>();
    private int currentIndex = 0;

    public LeftAdapter(List<String> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.left_list_item, null);
        return new MyHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.tv_name.setText(dataList.get(position));

        //分类的点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mleftListOnClickItemListener) {
                    mleftListOnClickItemListener.onItemClick(position);
                }
            }
        });

        if (currentIndex == position) {
            holder.itemView.setBackgroundResource(R.drawable.type_selector_bg);
        } else {
            holder.itemView.setBackgroundResource(R.drawable.type_selector_normal_bg);
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_name;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.type);
        }
    }

    private LeftListOnClickItemListener mleftListOnClickItemListener;

    public void setMleftListOnClickItemListener(LeftListOnClickItemListener mleftListOnClickItemListener) {
        this.mleftListOnClickItemListener = mleftListOnClickItemListener;
    }

    public interface LeftListOnClickItemListener {
        void onItemClick(int position);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setCurrentIndex(int position) {
        this.currentIndex = position;
        notifyDataSetChanged();
    }

}
